package com.wefans.wefansapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.google.gson.Gson;
import com.wefans.wefansapp.R;
import com.wefans.wefansapp.common.tool.data.MFDataManager;
import com.wefans.wefansapp.common.tool.data.ResGetCountry;
import com.wefans.wefansapp.common.tool.debug.MFDebug;
import com.wefans.wefansapp.common.tool.network.MFNetworkManager;
import com.wefans.wefansapp.common.tool.network.MFNetworkManager.IOnNetworkListener;
import com.wefans.wefansapp.common.tool.network.model.NetJsonData;
import com.wefans.wefansapp.common.tool.network.model.NetMessage;
import com.wefans.wefansapp.common.tool.preference.MFSharedPreference;

public class IntroActivity extends BaseActivity implements IOnNetworkListener {

    private static long        INTRO_LOADING_TIME = 1000*2;//인트로 로딩 타임은 2초로 설정(단, 초기 데이터를 네트워크 연동은 마쳐야 한다.)
    
	private MFSharedPreference mPrf               = null;
	private MFNetworkManager   m_oNetworkManager  = null;
    private MFDataManager      m_oDataManager     = null;
    
    private long               m_lNetRequestTime  =   0L;
    private long               m_lNetResponseTime =   0L;
	
	Handler mHandler = new Handler()
	{
		@Override
		public void handleMessage(Message msg) {

			Intent it = null;
			
			if (mPrf.isFirstLaunch()) {
				createShoutcut();
				it = new Intent(IntroActivity.this, TutorialActivity.class);
				mPrf.setFirstLaunch(false);
			} else {
				it = new Intent(IntroActivity.this, LoginActivity.class);
			}
			
			if (it != null) {
				it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
//				it.putExtra(AppSt.GCM_URL, URL);
				startActivity(it);
				finish();
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_intro);
		
		mPrf = new MFSharedPreference(this);
		m_oNetworkManager = MFNetworkManager.getInstance(this);
		m_oDataManager    = MFDataManager.getInstance(this);
		
		m_lNetRequestTime = System.currentTimeMillis();
		m_oNetworkManager.requestGetCountry(this);
	}
	
	private void createShoutcut() {
		
		Intent actionIntent = new Intent(Intent.ACTION_MAIN);
		actionIntent.setComponent(getComponentName());
		actionIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK	| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

		Intent shortcutIntent = new Intent();
		shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, actionIntent);
		shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,	getString(R.string.app_name));
		shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, 
								Intent.ShortcutIconResource.fromContext(IntroActivity.this,	R.drawable.ic_launcher));
		shortcutIntent.putExtra("duplicate", false);
		shortcutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
		sendBroadcast(shortcutIntent);		
	}

    @Override
    public void onNetMessage(int resultCode, NetMessage message)
    {
        try {
            if( message instanceof NetJsonData ) {
                long magicNum = ((NetJsonData) message).getMagicNum();
                String json = ((NetJsonData) message).getResponseJson();
                
                if( json == null ) {
                    throw new Exception("invalid json data");
                }
                
                Gson gson = new Gson();
                
                if( magicNum == MFNetworkManager.REQUEST_GET_COUNTRY ) {
                    ResGetCountry country = (ResGetCountry) gson.fromJson(json, ResGetCountry.class);
                    if( country == null ) {
                        throw new Exception("Gson parsing error");
                    }
                    
                    m_oDataManager.setCountryInfo(country);
                    
                    m_lNetResponseTime = System.currentTimeMillis();
                    
                    long lDelayTime = INTRO_LOADING_TIME - (m_lNetResponseTime - m_lNetRequestTime);
                    
                    if( lDelayTime > 0 ) {
                        mHandler.sendEmptyMessageDelayed(0, lDelayTime);      
                    } else {
                    	lDelayTime = 0;
                    	mHandler.sendEmptyMessageDelayed(0, lDelayTime);
                    }
                }
            }
        } catch (Exception e) {
            MFDebug.e(e);
        }
    }
}
